package com.tanhua.commons.huanxin.utils;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.huanxin.config.HuanXinConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Component
public class HuanXinUtils {

    @Autowired
    private HuanXinConfig config;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 注册环信用户
     */
    public boolean register(Long userId) {

        String url = config.getHuanXinUrl() + "/users";

        String token = this.tokenUtils.getToken();

        //请求头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        httpHeaders.add("Authorization", "Bearer " + token);

        List<HuanXinUser> huanXinUsers = new ArrayList<>();
        huanXinUsers.add(new HuanXinUser(userId.toString(), "123456"));

        try {
            HttpEntity<String> httpEntity = new HttpEntity(JSON.toJSONString(huanXinUsers), httpHeaders);
            //发起请求
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            return responseEntity.getStatusCodeValue() == 200;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 添加好友
     */
    public boolean contactUsers(Long userId, Long friendId) {

        String targetUrl = config.getHuanXinUrl()+ "/users/" + userId + "/contacts/users/" + friendId;

        String token = this.tokenUtils.getToken();

        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json ");
        headers.add("Authorization", "Bearer " + token);

        try {
            HttpEntity<String> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(targetUrl, httpEntity, String.class);
            return responseEntity.getStatusCodeValue() == 200;
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 添加失败
        return false;
    }

    //发送消息
    public Boolean sendMsg(String target, String msg) {

        String targetUrl =config.getHuanXinUrl() + "/messages";

        String token = this.tokenUtils.getToken();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json ");
        headers.add("Authorization", "Bearer " + token);

        try {
            //请求头
            Map<String, Object> requestParam = new HashMap<>();
            requestParam.put("target_type", "users");
            requestParam.put("target", Arrays.asList(target));

            Map<String, Object> msgParam = new HashMap<>();
            msgParam.put("msg", msg);
            msgParam.put("type", "txt");

            requestParam.put("msg", msgParam);

            //表示消息发送者;无此字段Server会默认设置为“from”:“admin”，有from字段但值为空串(“”)时请求失败
            //requestParam.put("from", null);

            HttpEntity<Map> httpEntity = new HttpEntity<>(requestParam, headers);

            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(targetUrl, httpEntity, String.class);

            return responseEntity.getStatusCodeValue() == 200;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
